import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import Dashboard from '@/views/Dashboard.vue'
import { useAuthStore } from '@/store'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Dashboard,
    meta: { requiresAuth: true },
    children: [
      // 患者路由
      {
        path: 'patient',
        name: 'PatientDashboard',
        component: () => import('@/components/Dashboard/PatientDashboard.vue'),
        meta: { role: 'patient' },
        children: [
          {
            path: '',
            name: 'PatientOverview',
            component: () => import('@/components/Dashboard/PatientOverview.vue'),
          },
          {
            path: 'profile',
            name: 'PatientProfile',
            component: () => import('@/components/Dashboard/PatientProfile.vue'),
          },
          {
            path: 'records',
            name: 'PatientRecords',
            component: () => import('@/components/Dashboard/PatientRecords.vue'),
          },
          {
            path: 'appointments',
            name: 'PatientAppointments',
            component: () => import('@/components/Dashboard/PatientAppointments.vue'),
          },
          {
            path: 'account',
            name: 'PatientAccount',
            component: () => import('@/components/Dashboard/PatientAccount.vue'),
          }
        ]
      },
      // 医生路由
      {
        path: 'doctor',
        name: 'DoctorDashboard',
        component: () => import('@/components/Dashboard/DoctorDashboard.vue'),
        meta: { role: 'doctor' },
        children: [
          {
            path: '',
            name: 'DoctorOverview',
            component: () => import('@/components/Dashboard/DoctorOverview.vue'),
          },
          {
            path: 'profile',
            name: 'DoctorProfile',
            component: () => import('@/components/Dashboard/DoctorProfile.vue'),
          },
          {
            path: 'patients',
            name: 'DoctorPatients',
            component: () => import('@/components/Dashboard/DoctorPatients.vue'),
          },
          {
            path: 'records',
            name: 'DoctorRecords',
            component: () => import('@/components/Dashboard/DoctorRecords.vue'),
          },
          {
            path: 'schedule',
            name: 'DoctorSchedule',
            component: () => import('@/components/Dashboard/DoctorSchedule.vue'),
          },
          {
            path: 'account',
            name: 'DoctorAccount',
            component: () => import('@/components/Dashboard/DoctorAccount.vue'),
          }
        ]
      },
      // 管理员路由
      {
        path: 'admin',
        name: 'AdminDashboard',
        component: () => import('@/components/Dashboard/AdminDashboard.vue'),
        meta: { role: 'admin' },
        children: [
          {
            path: '',
            name: 'AdminOverview',
            component: () => import('@/components/Dashboard/AdminOverview.vue'),
          },
          {
            path: 'users',
            name: 'AdminUsers',
            component: () => import('@/components/Dashboard/AdminUsers.vue'),
          },
          {
            path: 'hospitals',
            name: 'AdminHospitals',
            component: () => import('@/components/Dashboard/AdminHospitals.vue'),
          },
          {
            path: 'departments',
            name: 'AdminDepartments',
            component: () => import('@/components/Dashboard/AdminDepartments.vue'),
          },
          {
            path: 'blockchain',
            name: 'AdminBlockchain',
            component: () => import('@/components/Dashboard/AdminBlockchain.vue'),
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const isAuthenticated = localStorage.getItem('isAuthenticated') === 'true'
  const userRole = localStorage.getItem('userRole')
  
  // 设置状态
  if (isAuthenticated) {
    authStore.isAuthenticated = true
    authStore.userRole = userRole || ''
  }
  
  // 需要认证但未登录
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } 
  // 角色权限验证
  else if (to.meta.role && userRole !== to.meta.role) {
    next(`/dashboard/${userRole}`)
  } 
  // 否则放行
  else {
    next()
  }
})

export default router